<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/8/1
 * Time: 16:52
 * 视频管理
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Video extends Base {
        public function index(){
            if(request()->isAjax()){
                extract(input());
                $map =array();
                $page = input('page') ? input('page'):1;
                $limit = input("limit")?input("limit"):10;// 获取总条数;
                if(isset($keyword)&&$keyword!="")
                {
                    $map['uuu.user_username'] = ['like',"%" . $keyword . "%"];
                }
                if(isset($video_status)&&$video_status!="")
                {
                    $map['a.video_status'] = $video_status;
                }
                if(isset($information_type)&&$information_type!="")
                {
                    $map['a.video_type_id'] = $information_type;
                }
                $count = Db::table('up_video')->alias('a')
                    ->join('up_video_type uit','uit.video_type_id = a.video_type_id','left')
                    ->join('up_u_user uuu','uuu.user_id = a.video_user_id','left')
                    ->where($map)
                    ->count();
                $list = Db::table('up_video')->alias('a')
                    ->join('up_video_type uit','uit.video_type_id = a.video_type_id','left')
                    ->join('up_u_user uuu','uuu.user_id = a.video_user_id','left')
                    ->where($map)
                    ->field('a.*,uit.video_type_name,uuu.user_username')
                    ->order('a.video_time desc')
                    ->page($page, $limit)
                    ->select();//计算总页面
                foreach ($list as $key=>&$vo){
                    $where1['type'] =5;
                    $where1['f_id'] =$vo['video_id'];
                    $count1 = Db::table('up_comment')->where($where1)->count();
                    $vo['num'] = $count1;
                }
                return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
            }
            $info  = Db::table('up_video_type')->select();
            $this->assign('info',$info);
            return $this->fetch();
        }

    //删除
    public function del(){
        $id  = input('param.id');
        $where['type'] = 2;
        $where['f_id'] = $id;
        $info = Db::table('up_comment')->where($where)->count();
        if($info>0){
            Db::table('up_comment')->where($where)->delete();
        }
        $res = Db::table('up_video')->where('video_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    //通过
    public function adopt(){
        $id = input('param.id');
        $res = Db::table('up_video')->where('video_id',$id)->setField('video_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    //驳回
    public function reject(){
        $id = input('param.id');
        $res = Db::table('up_video')->where('video_id',$id)->setField('video_status',3);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    //编辑
    public function edit(){
        if(Request::instance()->isPost()){
            $video_url = input('video_url');
            $id = input('param.id');
            $res = Db::table('up_video')->where('video_id', $id)
                ->update([
                    'videourl' => $video_url,
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $info = Db::table('up_video')->where('video_id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
}